<template>
  <a-layout class="admin-layout">
    <!-- 侧边栏 -->
    <a-layout-sider 
      :width="200" 
      class="layout-sider"
      :collapsed="false"
      breakpoint="lg"
    >
      <!-- Logo区域 -->
      <div class="logo-section">
        <div class="logo">
          <icon-settings class="logo-icon" />
          <span class="logo-text">管理系统</span>
        </div>
      </div>
      
      <!-- 菜单 -->
      <a-menu 
        :selected-keys="selectedKeys"
        :auto-open-selected="true"
        @menu-item-click="handleMenuClick"
        class="menu"
      >
        <a-menu-item key="/admin/scripts">
          <template #icon>
            <icon-file />
          </template>
          脚本管理
        </a-menu-item>
        
        <a-menu-item key="/admin/users">
          <template #icon>
            <icon-user />
          </template>
          用户管理
        </a-menu-item>
        
        <a-menu-item key="/admin/import">
          <template #icon>
            <icon-storage />
          </template>
          数据处理
        </a-menu-item>
      </a-menu>
    </a-layout-sider>
    
    <!-- 主内容区 -->
    <a-layout class="layout-content">
      <!-- 顶部导航栏 -->
      <a-layout-header class="layout-header">
        <div class="header-left">
          <a-breadcrumb class="breadcrumb">
            <a-breadcrumb-item>首页</a-breadcrumb-item>
            <a-breadcrumb-item>{{ currentPageTitle }}</a-breadcrumb-item>
          </a-breadcrumb>
        </div>
        
        <div class="header-right">
          <a-space :size="16">
            <!-- 搜索框 -->
  
            
            <!-- 用户下拉菜单 -->
            <a-dropdown trigger="click">
              <a-button type="text" class="user-btn">
                <template #icon>
                  <a-avatar :size="28">
                    {{ (userStore.user?.real_name || userStore.user?.username)?.charAt(0) }}
                  </a-avatar>
                </template>
                <span class="username">{{ userStore.user?.real_name || userStore.user?.username }}</span>
                <icon-down />
              </a-button>
              
              <template #content>
                <a-doption @click="handleLogout">
                  <template #icon>
                    <icon-poweroff />
                  </template>
                  退出登录
                </a-doption>
              </template>
            </a-dropdown>
          </a-space>
        </div>
      </a-layout-header>
      
      <!-- 内容区域 -->
      <a-layout-content class="main-content">
        <div class="content-wrapper">
          <router-view />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { Modal } from '@arco-design/web-vue'
import {
  IconSettings,
  IconFile,
  IconUser,
  IconStorage,
  IconPoweroff,
  IconDown
} from '@arco-design/web-vue/es/icon'
import { useUserStore } from '../stores/user'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()

const selectedKeys = ref([route.path])

const pageTitle = {
  '/admin/scripts': '脚本管理',
  '/admin/users': '用户管理',
  '/admin/import': '数据处理'
}

const currentPageTitle = computed(() => {
  return pageTitle[route.path as keyof typeof pageTitle] || '管理后台'
})

const handleMenuClick = (key: string) => {
  router.push(key)
}

const handleLogout = () => {
  Modal.confirm({
    title: '确认退出',
    content: '您确定要退出登录吗？',
    onOk: () => {
      userStore.logout()
      router.push('/login')
    }
  })
}

watch(
  () => route.path,
  (newPath) => {
    selectedKeys.value = [newPath]
  }
)
</script>

<style scoped>
.admin-layout {
  min-height: 100vh;
  background: #f5f5f5;
}

/* 侧边栏样式 */
:deep(.layout-sider) {
  background: #fff;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  border-right: 1px solid #e5e6eb;
}

.logo-section {
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #e5e6eb;
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-icon {
  font-size: 20px;
  color: #165dff;
}

.logo-text {
  font-size: 16px;
  font-weight: 600;
  color: #1d2129;
}

/* 菜单样式 */
:deep(.menu) {
  background: #fff;
  border: none;
  padding: 16px 0;
}

:deep(.arco-menu-item) {
  margin: 4px 12px;
  border-radius: 6px;
  color: #4e5969;
  font-size: 14px;
  height: 40px;
  line-height: 40px;
}

:deep(.arco-menu-item:hover) {
  background: #f2f3f5;
  color: #1d2129;
}

:deep(.arco-menu-item-selected) {
  background: #e8f3ff;
  color: #165dff;
  font-weight: 500;
}

:deep(.arco-menu-item-selected::after) {
  display: none;
}

/* 主内容区样式 */
.layout-content {
  background: #f5f5f5;
}

.layout-header {
  background: #fff;
  border-bottom: 1px solid #e5e6eb;
  height: 64px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.04);
}

.header-left {
  flex: 1;
}

.breadcrumb {
  color: #4e5969;
}

:deep(.arco-breadcrumb-item-link) {
  color: #4e5969;
  font-size: 14px;
}

:deep(.arco-breadcrumb-item:last-child .arco-breadcrumb-item-link) {
  color: #1d2129;
  font-weight: 500;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-btn {
  padding: 4px 8px;
  color: #4e5969;
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-size: 14px;
  color: #1d2129;
}

/* 内容区域 */
.main-content {
  padding: 24px;
  min-height: calc(100vh - 64px);
}



/* 响应式设计 */
@media (max-width: 768px) {
  :deep(.layout-sider) {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    z-index: 1000;
  }
  
  .layout-header,
  .main-content {
    margin-left: 0;
  }
}
</style>
